package cn.spike.system.service.impl;

import cn.spike.org.domain.Department;
import cn.spike.system.domain.Menu;
import cn.spike.system.mapper.MenuMapper;
import cn.spike.system.service.IMenuService;
import cn.spike.basic.service.impl.BaseServiceImpl;
import com.sun.org.apache.bcel.internal.generic.ARRAYLENGTH;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 业务实现类：
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;


    @Override
    public List<Menu> getMenuTree() {
        Map<Long, Menu> map = new HashMap<>();
        //找出所有菜单信息
        List<Menu> menus = menuMapper.loadAll();
       //遍历放入map中
        for (Menu menu : menus) {
                map.put(menu.getId(),menu);
        }
        //2.遍历出所有部门 判断是否有上级部门id
        List<Menu> menusTree = new ArrayList<>();
        for (Menu menu : menus) {
            //如果它没有上级菜单
            if(menu.getParentId()==null){
                //他就是顶级菜单 - 直接放入菜单树中
                menusTree.add(menu);
            }else {
                //2.2 如果有上级菜单id 通过上级菜单id找到上级菜单 获取上级菜单的children属性 - 将自己添加进去
                Menu parentMenu = map.get(menu.getParentId());
                if(parentMenu != null){//判断如果删除了上级菜单 菜单为null导致空指针异常
                    parentMenu.getChildren().add(menu);//获取上级菜单的children属性 - 将自己添加进去
                }
            }
        }
        return menusTree;
    }

    @Override
    public List<ArrayList> getRowMenuTreeByRoleId(Long id) {
        //用来接收当前角色的所有菜单信息
        List<ArrayList> rowMenuTree = new ArrayList<>();
        //获取到这个角色所拥有的菜单
        List<Menu> rowMenus = menuMapper.getRowMenusByRoleId(id);

        //获取到所有的菜单
        List<Menu> menus = menuMapper.loadAll();
        Map<Long,Menu> allMenus = new HashMap<>();
        for (Menu menu : menus) {
            allMenus.put(menu.getId(),menu);
        }
        //遍历角色所拥有的菜单
        for (Menu rowMenu : rowMenus) {
            //空数组 接收每一组的菜单数组
            ArrayList<Long> oneMenu = new ArrayList<>();
            //获取到这一个菜单的父id
            Long parentId = rowMenu.getParentId();

            ArrayList<Long> oneMenuIds = new ArrayList<>();
            //先将自己放进去
            oneMenuIds.add(rowMenu.getId());
            //如果不为null 进入循环
            while (parentId!=null){
                //将父id加进去
                oneMenuIds.add(parentId);
                //在从map中获取到父亲对象
                Menu parentMenu = allMenus.get(parentId);
                parentId = parentMenu.getParentId();

            }
            for (int i = oneMenuIds.size()-1;i>=0;i--){
                oneMenu.add(oneMenuIds.get(i));
            }
            rowMenuTree.add(oneMenu);
        }
//        List<Menu> parentMenus = menuMapper.getRowMenuAndChildByRoleId(id);
//        for (Menu parentMenu : parentMenus) {
//            List<Menu> children = parentMenu.getChildren();
//            for (Menu child : children) {
//                ArrayList<Long> childId =  new ArrayList<>();
//                childId.add(parentMenu.getId());
//                childId.add(child.getId());
//                rowMenuTree.add(childId);
//            }
//        }
        return rowMenuTree;
    }


}
